package edu.gyc.data.struct.queue;



import java.util.Scanner;

public class CircleQueueDemo {
    public static void main(String[] args) {
        CircleQueue circleQueue = new CircleQueue(3);
        Scanner scanner = new Scanner(System.in);
        boolean loop=true;
        char key =' ';
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取数据");
            System.out.println("p(peek):查看队列头数据");
            System.out.println("n(num):查看队列有几个数据");
            key = scanner.next().charAt(0);//取第一个字符
            switch (key) {
                case 's':
                    circleQueue.show();
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    System.out.println("程序退出。");
                    System.exit(0);
                    break;
                case 'a':
                    System.out.println("请输入一个整数：");
                    int n=scanner.nextInt();
                    circleQueue.add(n);
                    break;

                case 'g':
                    try {
                        int t=circleQueue.pop();
                        System.out.println("取出的数据是："+t);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'p':
                    try {
                        int d=circleQueue.peek();
                        System.out.println("队列头部的数据是："+d);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;

                case 'n':
                    circleQueue.size();
                    break;
            }
        }
    }
}

class CircleQueue{
    private int maxSize;
    private int front; //队列头指向队列的第一个元素
    private int rear; //队列尾的最后一个元素的后一个位置
    private int[] queue;

    public CircleQueue(int maxSize) {
        this.maxSize=maxSize;
        queue = new int[maxSize];

    }

    public boolean isFull() {
        return (rear+1)%maxSize==front;
    }

    public boolean isEmpty() {
        return rear==front;
    }

    public void add(int n) {
        if (isFull()) {
            System.out.println("队列满了，无法加入数据。");
            return;
        }
        queue[rear]=n;
        //rear后移，考虑取模
        rear=(rear+1)%maxSize;

    }

    public int pop() {
        if (isEmpty()) {
            System.out.println("队列空，无数据可取。");

            throw new RuntimeException("队列空，无数据可取");
        }
        int t=queue[front];
        front=(front+1)%maxSize;
        return t;
    }

    public void show() {
        System.out.println("front="+front+" rear:"+rear);
        if (isEmpty()) {
            System.out.println("队列空，没有数据");
            return;
        }
        for (int i = front; i <front+size(); i++) {
            System.out.printf("queue[%d]=%d ", i%maxSize, queue[i%maxSize]);
        }

    }

    public int peek() {
        if (isEmpty()) {
            System.out.println("队列空，无数据可取。");
            throw new RuntimeException("队列空，无数据可取");
        }
        return queue[front];
    }

    public int size() {

       return (rear+maxSize-front)%maxSize;
    }
}
